define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/product/index',
                    add_url: 'product/product/add',
                    edit_url: 'product/product/edit',
                    del_url: 'product/product/del',
                    recyclebin_url: 'product/product/recyclebin',
                    multi_url: 'product/product/multi',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'name', title: __('Name'), operate: 'LIKE' },
                        { field: 'status', title: __('Status'), searchList: { "0": __('下架'), "1": __('上架') }, formatter: Table.api.formatter.flag },
                        { field: 'flag', title: __('Flag'), searchList: { "1": __('新品'), "2": __('热销'), "3": __('推荐') }, formatter: Table.api.formatter.flag },
                        { field: 'stock', title: __('Stock'), operate: 'LIKE' },
                        { field: 'price', title: __('Price'), operate: 'LIKE' },
                        { field: 'type.name', title: __('Typeid'), operate: 'LIKE' },
                        { field: 'unit.name', title: __('Unitid'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }
                    ]
                ],
                escape: false,
                // search:false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/product/recyclebin',
                    restore_url: 'product/product/restore',
                    del_url: 'product/product/destroy',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'name', title: __('Name'), operate: 'LIKE' },
                        // { field: 'status', title: __('Status'), searchList: { "0": __('下架'), "1": __('上架') }, formatter: Table.api.formatter.flag },
                        { field: 'flag', title: __('Flag'), searchList: { "1": __('新品'), "2": __('热销'), "3": __('推荐') }, formatter: Table.api.formatter.flag },
                        { field: 'stock', title: __('Stock'), operate: 'LIKE' },
                        { field: 'price', title: __('Price'), operate: 'LIKE' },
                        { field: 'type.name', title: __('Typeid'), operate: 'LIKE' },
                        { field: 'unit.name', title: __('Unitid'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'deletetime', title: __('Deletetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'restore',
                                    title: function (row) {
                                        return `${row.name}数据恢复`
                                    },
                                    icon: 'fa fa-reply',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                    confirm: '是否还原该商品？',
                                    extend: "data-toggle='tooltip'",
                                    success: function (data) {
                                        //ajax成功会刷新一下table数据列表
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ]
                        }
                    ]
                ],
                escape: false,
                // search:false,
            });

            $('.btn-restore').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                // console.log(ids);
                // return false;
                layer.confirm('确定要还原吗?', { title: '还原', btn: ['是', '否'] },
                    function (index) {
                        layer.close(index);
                        $.post("product/product/restore", { ids: ids, action: 'success', reply: '' }, function (response) {
                            if (response.code == 1) {
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            } else {
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function (index) {
                        layer.close(index);
                    }
                );

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});